package com.tianji.aigc.tools;

import com.tianji.aigc.config.ToolResultHolder;
import com.tianji.aigc.constants.Constant;
import com.tianji.aigc.tools.result.CourseInfo;
import com.tianji.api.client.course.CourseClient;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author wys
 * @version V1.0
 * @date 2025-04-23 10:12
 */
@Component
@RequiredArgsConstructor
public class CourseTools {
    private final CourseClient courseClient;

    @Tool(description = Constant.Tools.QUERY_COURSE_BY_ID)
    public CourseInfo queryCourseById(@ToolParam(description = Constant.ToolParams.COURSE_ID) Long courseId,
                                      ToolContext toolContext) {
        return Optional.ofNullable(courseId)
                .map(id -> CourseInfo.of(courseClient.baseInfo(id, true)))
                .map(v -> {
                    String key = String.valueOf(toolContext.getContext().get(Constant.REQUEST_ID));
                    String filed = "courseInfo" + courseId;
                    ToolResultHolder.put(key, filed, v);
                    return v;
                })
                .orElse(null);
    }


}
